package com.klhappy.houyuan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.klhappy.houyuan.R;
import com.klhappy.houyuan.adapter.AbleStampeAdapter;
import com.klhappy.houyuan.bean.BaseJsonBean;
import com.klhappy.houyuan.bean.FoodsStamp.FoodStampBean;
import com.klhappy.houyuan.constant.UrlConstant;
import com.klhappy.houyuan.interfaces.CallBackInterModle;
import com.klhappy.houyuan.modle.JiaYuanModle;
import com.klhappy.houyuan.utils.JsonCode;
import com.klhappy.houyuan.utils.NotifyMethod;
import com.klhappy.houyuan.view.AbsActivity;
import com.klhappy.houyuan.bean.FoodsStamp.Records;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by MrPu on 15/12/23.
 */
public class AbleStampActivity extends AbsActivity implements CallBackInterModle{
    public static final String ADG = "Food_StampActivity";
    @InjectView(R.id.able_stamp_list)
    ListView able_stamp_list;
    @InjectView(R.id.title)
    TextView title;

    @OnClick(R.id.click_ra)
    public void click_ra(){
        finish();
    }

    private JiaYuanModle modle;
	private FoodStampBean stampBean;
	private List<Records> list;
    private Intent intent;
    private AbleStampeAdapter adapter;
    private String money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_able_stamp);
        ButterKnife.inject(this);
        title.setText("后院粮票");
        modle = new JiaYuanModle(this);
        modle.setCallBackInterModle(this);
        modle.getFoods();
        showDialog("请稍等...");
        NotifyMethod.getInstance().addObserver(ADG, new NotifyMethod.NotifyListener() {
            @Override
            public void notify(Object o) {
                money = (String ) o;
                intent = new Intent(AbleStampActivity.this ,ConfirmOrderActivity.class);
                intent.putExtra("stamp", money);
                intent.putExtra("stamp_id" ,ForId(money) + "");
                setResult(30, intent);
                finish();
            }
        });
    }

    private int ForId(String num){
        int id = 0;
        for (int i = 0 ; i <list.size() ; i ++){
            if (list.get(i).getMoney() == num){
                id = list.get(i).getT_id();
            }
        }
        return id;
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            intent = new Intent(AbleStampActivity.this ,ConfirmOrderActivity.class);
            intent.putExtra("stamp", money);
            intent.putExtra("id" ,ForId(money));
            setResult(30, intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void CallBackFromModle(String url, BaseJsonBean o) {
        if (url.equals(UrlConstant.TICKETS_URL)){
            if (o.getCode() == 200){
				stampBean = JsonCode.getClass(o.getEntity() ,FoodStampBean.class);
				list = JsonCode.getClassList(stampBean.getRecords(), Records.class);
                try {
                    adapter = new AbleStampeAdapter(this, list);
                    able_stamp_list.setAdapter(adapter);
                }catch (Exception e){

                }
            }
            closeDialog();
        }
    }
}
